"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By


class BasePage:
    # litemall商城地址
    _BASE_URL = "https://litemall.hogwarts.ceshiren.com/"

    def __init__(self, driver: WebDriver = None):
        if driver is None:
            # 初始化浏览器
            # 使用 selenium grid节点运行
            # https://selenium-node.hogwarts.ceshiren.com/ui
            hogwarts_grid_url = "https://selenium-node.hogwarts.ceshiren.com/wd/hub"
            capabilities = {"browserName": "chrome", "browserVersion": "100.0"}
            self.driver = webdriver.Remote(
                command_executor=hogwarts_grid_url,
                desired_capabilities=capabilities)
            # self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(5)
            self.driver.maximize_window()

            # 打开litemall网站
            self.driver.get(self._BASE_URL)
        else:
            # 设置浏览器
            self.driver = driver

    def do_click(self, by: By, locator: str):
        # 点击操作
        self.driver.find_element(by, locator).click()

    def do_send_keys(self, value: str, by: By, locator: str):
        # 输入操作
        element = self.driver.find_element(by, locator)
        element.clear()
        element.send_keys(value)
